<?php
namespace app\api\controller;
// namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use Endroid\QrCode\Tests;
use qrcode;
use alisms\SmsDemo;
use think\File;
class UserController extends Controller
{

    /**
     * 返回用户个人信息
     * @return [type] [description]
     */
    public function user_account()
    {
        $uid = input('param.uid');
        $member_list = db('account')->where(['uid'=>$uid])->select();

        // dump($member_list);
        // exit;
        if (!empty($member_list)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$member_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }


    // $goods_arr['money']['integral'] = $account['integral'];
    // $goods_arr['money']['balance'] = $account['balance'];
    /**
     * 获取用户积分
     * @return [type] [description]
     */
    public function integral()
    {
        $uid = input('param.uid');
        $member_list = db('account')->where(['uid'=>$uid])->find();
        $integral_list = db('integral')->where(['uid'=>$uid])->select();
        $integral = [];
        $reduce_count = null;
        if (!empty($integral_list)) {
            foreach ($integral_list as $key => &$value) {
                $integral['list']['integral'] = $member_list['integral'];
                $reduce_count += $value['reduce'];
                $integral['integral'][$key] = $value;

            }
            $integral['list']['reduce_integral'] = $reduce_count;
        }else{
            $integral['list']['integral'] = $member_list['integral'];
            $integral['list']['reduce_integral'] = 0;
            $integral['integral']= [];
        }
        
        // dump($integral);
        // // dump($integral_list);
        // exit;
        // $list['integral'] = $member_list['integral'];
        if (!empty($integral)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$integral));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }


    /**
     * 获取用户余额
     * @return [type] [description]
     */
    public function balance()
    {
        $uid = input('param.uid');
        $member_list = db('account')->where(['uid'=>$uid])->find();
        $balance_list = db('balance')->where(['uid'=>$uid])->select();
        $balance = [];
        $reduce_count = null;
        if (!empty($balance_list)) {
            foreach ($balance_list as $key => $value) {
                $balance['list']['balance'] = $member_list['balance'];
                $reduce_count += $value['reduce'];
                $balance['balance'][$key] = $value;

            }
            $balance['list']['reduce_balance'] = $reduce_count;
        }else{
            $balance['list']['balance'] = $member_list['balance'];
            $balance['list']['reduce_balance'] = 0;
            $balance['balance']=[];
        }
        
       
        // $list['balance'] = $member_list['balance'];
        if (!empty($balance)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$balance));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }



    /**
     * 用户信息修改
     * @return [type] [description]
     */
    public function account_edit()
    {
        // echo '111';
        $uid = input('param.uid');
        $account = db('account')->where(['uid'=>$uid])->find();
        $list['age'] = input('param.age')? input('param.age'):$account['age'];
        $list['sex'] = input('param.sex')? input('param.sex'):$account['sex'];
        $list['date'] = input('param.date')? input('param.date'):$account['date'];
        // echo $date;
        $list['vocation'] = input('param.vocation')?input('param.vocation'):$account['vocation'];
        // dump($list);
        // exit;
        // $age = input('param.age');??'0';
        if (db('account')->where(['uid'=>$uid])->update($list) > 0) {
            $a_list = db('account')->where(['uid'=>$uid])->select();
            echo json_encode(array("status"=>1,'message'=>'修改成功!',"data"=>$a_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'修改失败!',"data"=>[]));
            exit;
        }

    }

    /**
     * 修改用户手机号验证码
     * @return [type] [description]
     */
    public function phone_code()
    {
        include_once ROOT_PATH.'extend/aliyun-dysms-php-sdk/api_demo/smsdemo.php';
        // dump(input('param.'));
        $phone = input('param.phone');
        if (empty($phone)) {
            echo json_encode(array("status"=>6,'message'=>'手机号码不能为空',"data"=>[]));
            exit;
        }
        $TemplateCode = 'SMS_142384202';
        $Verification_one = rand(1000,9999);
        $Verification_one = (string)$Verification_one;
        $vas_phone = Db::name('verification_edit')->where(['phone'=>$phone])->find();
        if (empty($vas_phone)) {
            // $list['ip']    = $_SERVER["REMOTE_ADDR"];
            $list['phone'] = $phone;
            $list['vas']   = $Verification_one;
            $list['time']  = date('Y-m-d H:i');
            db('verification_edit')->insert($list);
        }else{
            $list['vas']    = $Verification_one;
            $list['time']   = date('Y-m-d H:i');
            db('verification_edit')->where(array('phone'=>$phone))->update($list);    
        }

        $Verification = $Verification_one;
        $response = SmsDemo::sendSms($phone,$TemplateCode,$Verification);
        if ($response) {
            echo json_encode(array("status"=>1,'message'=>'短信验证码发送成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'短信验证码发送失败',"data"=>[]));
            exit;
        }
        exit;
    }



    /**
     * 修改用户手机号
     * @return [type] [description]
     */
    public function phone_edit()
    {
        $vas = input('param.code');
        $phone = input('param.phone');
        $uid = input('param.uid');
        $member_list = db('member')->where(['uid'=>$uid])->find();
        $vas_list = Db::name('verification_edit')->where(['phone'=>$phone])->find();
        // dump($vas_list);
        if ($vas != $vas_list['vas']) {
            //如果验证码不正确 ，返回false;
            echo json_encode(array("status"=>3,'message'=>'短信验证码不正确',"data"=>[]));
            exit;
        }

        //修改内容
        $edit_member_phone['phone'] = $phone;

        //查询条件
        $member_where = [
          'uid' => $uid,
          'phone' => $member_list['phone']
        ];
        if (db('member')->where($member_where)->update($edit_member_phone) > 0) {
            $member_type = '1';
        }

        //修改内容
        $edit_account_phone['phone'] = $phone;
        //查询条件
        $account_where = [
          'uid' => $uid,
          'phone' => $member_list['phone']
        ];
        if (db('account')->where($account_where)->update($edit_account_phone) > 0) {
            $account_type = '1';
        }

        //修改内容
        $edit_order_phone['member_phone'] = $phone;
        //查询条件
        $order_where = [
          // 'uid' => $uid,
          'member_phone' => $member_list['phone']
        ];

        if (db('goods_order')->where($order_where)->update($edit_order_phone) > 0) {
            $order_type = '1';
        }
        
        if ($member_type == '1' && $account_type == '1' && $order_type == '1') {

            echo json_encode(array("status"=>1,'message'=>'修改手机号成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'修改手机号失败',"data"=>[]));
            exit;
        }

        // $list['time'] = date('Y-m-d H:i');
    }





    /**
     * 修改用户密码短信验证码
     * @return [type] [description]
     */
    public function pwd_code()
    {
        include_once ROOT_PATH.'extend/aliyun-dysms-php-sdk/api_demo/smsdemo.php';
        // dump(input('param.'));
        $phone = input('param.phone');
        if (empty($phone)) {
            echo json_encode(array("status"=>6,'message'=>'手机号码不能为空',"data"=>[]));
            exit;
        }
        $TemplateCode = 'SMS_142384201';
        $Verification_one = rand(1000,9999);
        $Verification_one = (string)$Verification_one;
        $vas_phone = Db::name('verification_pwd')->where(['phone'=>$phone])->find();
        if (empty($vas_phone)) {
            // $list['ip']    = $_SERVER["REMOTE_ADDR"];
            $list['phone'] = $phone;
            $list['vas']   = $Verification_one;
            $list['time']  = date('Y-m-d H:i');
            db('verification_pwd')->insert($list);
        }else{
            $list['vas']    = $Verification_one;
            $list['time']   = date('Y-m-d H:i');
            db('verification_pwd')->where(array('phone'=>$phone))->update($list);    
        }

        $Verification = $Verification_one;
        $response = SmsDemo::sendSms($phone,$TemplateCode,$Verification);
        if ($response) {
            echo json_encode(array("status"=>1,'message'=>'短信验证码发送成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'短信验证码发送失败',"data"=>[]));
            exit;
        }
        exit;
    }

    /**
     * 修改用户密码
     * @return [type] [description]
     */
    public function pwd_edit()
    {
        $vas = input('param.code');
        $phone = input('param.phone');
        // $uid = input('param.uid');
        $pwd['pwd'] = md5(md5(input('param.pwd')));
        $member_list = db('member')->where(['phone'=>$phone])->find();
        $vas_list = Db::name('verification_pwd')->where(['phone'=>$phone])->find();
        // dump($vas_list);
        if ($vas != $vas_list['vas']) {
            //如果验证码不正确 ，返回false;
            echo json_encode(array("status"=>3,'message'=>'短信验证码不正确',"data"=>[]));
            exit;
        }else{
            if (db('member')->where(['phone'=>$phone])->update($pwd) > 0) {
                echo json_encode(array("status"=>1,'message'=>'密码修改成功',"data"=>[]));
                exit;
            }else{
                echo json_encode(array("status"=>2,'message'=>'密码修改失败',"data"=>[]));
                exit;
            }
        }



    }





    public function home()
    {
         $file = request()->file('img');
        dump($file);
        exit;
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                //获取图片路径
                $list['img'] =  'uploads/'.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }



    /**
     * 用户收货地址
     * @return [type] [description]
     */
    public function address_index()
    {
        $uid = input('param.uid');
        $addressList = db('address')->where(['uid'=>$uid])->order('type desc')->select();
        // dump($addressList);
        // exit;
        if (!empty($addressList)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功',"data"=>$addressList));
            exit;
        }else{
            echo json_encode(array("status"=>1,'message'=>'数据为空',"data"=>[]));
            exit;
        }
    }


    /**
     * 新增收货地址
     * @return [type] [description]
     */
    public function address_add()
    {
        $address_list = input('param.');
        $uid = input('param.uid');
        $addressList = db('address')->where(['uid'=>$uid])->select();
        if (empty($addressList)) {
            $address_list['type'] = '1';
        }

        if (db('address')->insert($address_list) > 0) {
            echo json_encode(array("status"=>1,'message'=>'添加成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'添加失败',"data"=>[]));
            exit;
        }

    }


    /**
     * 修改收货地址
     * @return [type] [description]
     */
    public function address_edit()
    {
        $list = input('param.');
        $addressList = db('address')->where(['uid'=>$list['uid'],'aid'=>$list['aid']])->find();

        $address_list['uname'] = input('param.uname')? input('param.uname'):$addressList['uname'];
        $address_list['phone'] = input('param.phone')? input('param.phone'):$addressList['phone'];
        $address_list['s_province'] = input('param.s_province')? input('param.s_province'):$addressList['s_province'];
        $address_list['s_city'] = input('param.s_city')? input('param.s_city'):$addressList['s_city'];
        $address_list['s_county'] = input('param.s_county')? input('param.s_county'):$addressList['s_county'];
        $address_list['address'] = input('param.address')? input('param.address'):$addressList['address'];
        if (db('address')->where(['aid'=>$list['aid']])->update($address_list) > 0) {
            echo json_encode(array("status"=>1,'message'=>'修改成功',"data"=>$address_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'修改失败',"data"=>[]));
            exit;
        }
    }


    /**
     * 设置默认地址
     * @return [type] [description]
     */
    public function address_type()
    {
        $list = input('param.');
        $address_type_id = db('address')->where(['uid'=>$list['uid'],'type'=>'1'])->find();
        $unsettype['type'] = '0';
        $settype['type'] = '1';
        db('address')->where(['aid'=>$address_type_id['aid']])->update($unsettype);
        if (db('address')->where(['aid'=>$list['aid']])->update($settype) > 0) {
            echo json_encode(array("status"=>1,'message'=>'修改成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'修改失败',"data"=>[]));
            exit;
        }
        // dump()
        
    }


    /**
     * 删除收货地址
     * @return [type] [description]
     */
    public function address_del()
    {
    	$aid = input('param.aid');
        $addressUid = db('address')->where(['aid'=>$aid])->find();
        $addressList = db('address')->where(['uid'=>$addressUid['uid'],'aid'=>['neq',$aid]])->order('aid desc')->select();
        $type['type'] = '1';
        if (!empty($addressList)) {
            if ($addressUid['type'] == '1') {
                db('address')->where(['aid'=>$addressList[0]['aid']])->update($type);
            }
        }
        
    	if (db('address')->where(['aid'=>$aid])->delete() > 0) {
    		echo json_encode(array("status"=>1,'message'=>'删除成功',"data"=>[]));
            exit;
    	}else{
    		echo json_encode(array("status"=>2,'message'=>'删除失败',"data"=>[]));
            exit;
    	}
    }




    /**
     * 头像上传
     */
    public function user_img()
    {
        $uid = input('param.uid');
        $file = request()->file('image');
        // dump($file);
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            // dump($info);
            // exit;
            if($info){
                //获取图片路径
                $update['user_img'] =  'http://www.mochoushop.com/uploads/'.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        if (db('account')->where(['uid'=>$uid])->update($update) > 0) {
            echo json_encode(array("status"=>1,'message'=>'上传成功',"data"=>[]));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'上传失败',"data"=>[]));
            exit;
        }
    }




    /**
     * 
     */
    public function user_name()
    {
        $list = input('param.');
        $addressList = db('account')->where(['uid'=>$list['uid']])->find();

        $address_list['user_name'] = input('param.user_name')? input('param.user_name'):$addressList['user_name'];
        
        if (db('account')->where(['uid'=>$list['uid']])->update($address_list) > 0) {
            echo json_encode(array("status"=>1,'message'=>'修改成功',"data"=>$address_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'修改失败',"data"=>[]));
            exit;
        }
    }

    
}
